<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
	<!--Con//Start-->
	<div class="mar-st-a30" style="width: 400px">
		<form id="dataForm" name="dataForm" method="post"
			action="{{ctx}}/mam/widget/update" enctype="multipart/form-data">
			<input name="checkIdentify" class="input-common" type="hidden"
				id="showResult" value="" /> <input class="input-common"
				type="hidden" id="name_showResult" value="" /> <input
				class="input-common" type="hidden" id="autoShowResult" value="" />
			<div>
				<input name="id" class="input-common" type="hidden" id="id"
					value="{{widgetVO.id}}" />
			</div>
			<div class="mar-st-a30">
				<table width="100%" border="0" align="center" cellpadding="0"
					cellspacing="0" class="tab-td-pa">
					<tr>
						<td align="right" class="bd_txt">模块类别：</td>
						<td class="text-left"><select name="category_id"
							id="category_id" class="form-control"> {{#list}}
								<option value="{{id}}">{{name}}</option> {{/list}}
						</select></td>
					</tr>
					<tr>
						<td width="30%" align="right" for="focusedInput">应用权限：</td>
						<td class="text-left"><select name="appAuth"
							class="form-control" id="appAuth">
								<option value="false">否</option>
								<option value="true">是</option>
						</select></td>
					</tr>
					<tr>
						<td width="30%" align="right" for="focusedInput">模块名称：</td>
						<td class="text-left"><input class="form-control" name="name"
							onblur="checkName()" type="text" value="{{widgetVO.name}}"
							id="name" btvd-type="required"></td>
					</tr>
					<tr>
						<td width="30%" align="right" for="focusedInput">模块标识：</td>
						<td class="bd_sr"><input class="form-control" type="text"
							name="identify" value="{{widgetVO.identify}}" id="identifier"
							readonly="readonly" /></td>
					</tr>
					<tr id="localtr" style="display: none">
						<td width="30%" align="right" for="focusedInput">local标识：</td>
						<td class="text-left"><input class="form-control" type="text"
							onblur="checkLocal()" name="local" value="{{widgetVO.local}}"
							id="local" btvd-type="required" /></td>
					</tr>

					<tr id="iconUploadContainertr" style="display:">
						<td width="30%" align="right" for="focusedInput">模块图标：</td>
						<td class="text-left"><div id="iconUploadContainer"></div></td>
					</tr>
					<tr>
						<td width="30%" align="right" for="focusedInput">是否显示：</td>
						<td class="text-left"><input type="radio"
							class="radio-inline" onclick="isShowCheck()" name="hidden"
							id="hiddenYES" value="true" />隐藏 <input type="radio"
							class="radio-inline" name="hidden" id="hiddenNO"
							onclick="isShowCheck()" value="false" />显示</td>
					</tr>
					<tr id="autoDownloadtr" style="display:">
						<td width="30%" align="right" for="focusedInput">是否自动下载：</td>
						<td class="text-left"><input type="radio"
							class="radio-inline" name="autoDownload" id="autoDownloadNO"
							value="false" />否 <input type="radio" class="radio-inline"
							name="autoDownload" id="autoDownloadYES" value="true" />是</td>
						<script type="text/javascript">
							var autoDownload = "{{widgetVO.autoDownload}}";
							if (autoDownload == "true") {
								jQuery("#autoDownloadYES").attr("checked",
										"true");
							} else {
								jQuery("#autoDownloadNO").attr("checked",
										"true");
							}
						</script>
					</tr>
					<tr id="isAutoShowChecktr" style="display:">
						<td width="30%" align="right" for="focusedInput">是否自动打开：</td>
						<td class="text-left"><input type="radio"
							class="radio-inline" name="autoShow" id="isAutoShowNO"
							value="false" checked="checked" onclick="isAutoShowCheck()" />否
							<input type="radio" class="radio-inline" name="autoShow"
							id="isAutoShowYes" value="true" onclick="isAutoShowCheck()" />是</td>
					</tr>
					<tr id="timeUnit_isAutoShowYes" style="display: none">
						<td width="30%" align="right" for="focusedInput">单位时间：</td>
						<td class="text-left"><select name="timeUnit" id="timeUnit"
							class="form-control">
								<option value="H">小时</option>
								<option value="M">分钟</option>
								<option value="S">秒</option>
						</select></td>
					</tr>
					<tr id="showIntervalTime_isAutoShowYes" style="display: none">
						<td width="30%" align="right" for="focusedInput">时间间隔：</td>
						<td class="text-left"><input class="form-control"
							onblur="checkNumber('showIntervalTimes')" name="showIntervalTime"
							value="{{widgetVO.showIntervalTime}}" id="showIntervalTimes"
							btvd-type="required" /></td>
					</tr>
					<tr>
						<td width="30%" align="right" for="focusedInput">权重值：</td>
						<td class="text-left"><input class="form-control"
							name="sortingWeight" value="{{widgetVO.sortingWeight}}"
							onblur="checkNumber('sortingWeight')" id="sortingWeight"
							btvd-type="required" /></td>
					</tr>

					<tr>
						<td align="right" class="control-group">模块简介：</td>
						<td colspan="3" class="text-left"><textarea id="discription"
								class="form-control" name="discription" cols="" rows="">{{widgetVO.discription}}</textarea></td>
					</tr>
				</table>
			</div>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	$('#dataForm').validation();
	$("#category_id option[value='{{widgetVO.categoryVO.id}}']").attr(
			"selected", true);
	$("#appAuth option[value='{{widgetVO.appAuth}}']").attr(
			"selected", true);
	$("#timeUnit option[value='{{widgetVO.timeUnit}}']").attr("selected", true);
	//判断是否隐藏
	var hidden = "{{widgetVO.hidden}}";
	if (hidden == "true") {
		jQuery("#hiddenYES").attr("checked", "true");
		$('#isAutoShowChecktr').hide();
	} else {
		jQuery("#hiddenNO").attr("checked", "true");
		$('#isAutoShowChecktr').show();
	}
	//判断是不是本地模块
	var oldLocalstr = "{{widgetVO.local}}";
	if (oldLocalstr != "") {
		$('#localtr').show();
		$('#autoDownloadtr').hide();
		$('#iconUploadContainertr').hide();
		$('#isAutoShowChecktr').hide();
	}else{
		$("#local").removeAttr("btvd-type");
	}

	//判断是否隐藏
	function isShowCheck() {
		var autoShow = $('#hiddenYES');
		if (autoShow.is(':checked')) {
			$("input[name='autoShow']").get(0).checked = true;
			$('#isAutoShowChecktr').hide();
			$('#timeUnit_isAutoShowYes').hide();
			$('#showIntervalTime_isAutoShowYes').hide()
		} else {
			$('#isAutoShowChecktr').show();
		}
		if(oldLocalstr != ""){//是本地模块
			$('#isAutoShowChecktr').hide();
		}
	}

	var isAutoShow = "{{widgetVO.autoShow}}";
	if (isAutoShow == "true") {
		$('#isAutoShowYes').attr("checked", true);
		$('#timeUnit_isAutoShowYes').show();
		$('#showIntervalTime_isAutoShowYes').show();
	} else {
		$('#isAutoShowNo').attr("checked", true);
	}
	$(document).ready(function() {
		var uploader = buildPlupload({
			id : "iconUploadContainer",
			elmName : "icon_id",
            filters : [{title : "图标", extensions : "png"}],
            tips:"请上传png格式图片",
			targetUrl : "{{ctx}}/mam/attachment/icon",
			defaultVal : "{{widgetVO.icon_id}}",
			btnName : "上传图标"
		});
	});

	function formSubmit() {
		checkName();
		var validateError=false;
		$('input, textarea').each(function() {
            var el = $(this), valid = el.attr('btvd-err');
            if (valid != null && valid.length > 0 ) {                       
            	validateError=true;
            	
            	return;
            }
        });
		if(validateError){
			return;
		}
		
		if ($('#autoShowResult').val() != "") {
			updateAutoShow($('#autoShowResult').val());
		} else {
			saveData();
		}

	}

	function saveData() {
		var data = $('#dataForm').serialize();
		$.ajax({
			async : true,
			type : "POST",
			url : "{{ctx}}/mam/widget/update",
			data : data,
			dataType : 'json',
			success : function(result, textStatus) {
				if (result.result == "success") {
					bootbox.alert("更新成功！", function() {
						window.location.reload();
					});
				} else {
					bootbox.alert("更新失败，请稍等再试！");

				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});
	}
	
	function checkLocal(){
		if(oldLocalstr == ""){
			$("#local").removeAttr("btvd-type");
			$("#local").removeAttr("btvd-err");
			return;
		}
		
		if($("#local").val() == ""){
			 $("#local").attr("btvd-type","required");
			 $("#local").attr("btvd-err","请输入内容");
		}else{
			 $("#local").removeAttr("btvd-type");
			 $("#local").removeAttr("btvd-err");
		}
	}

	function checkName() {
		var name = $("#name").val();
		var oldName = "{{widgetVO.name}}";
		if (oldName != name) {
		if(name==""){
			 $("#name").attr("btvd-type","required");
			 $("#name").attr("btvd-err","请输入内容");
			return;
		}
		 var tel =/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;'\,.<>?]{0,19}$/; 
		 var flag = tel.test(name);
		 if(!flag){
			 $("#name").attr("btvd-type","noSpecialChar");
			 $("#name").attr("btvd-err","名称不允许包含特殊字符，请换别的试试");
			 return;
		 }else{
			 $("#name").removeAttr("btvd-type");
				$("#name").removeAttr("btvd-err");
		 }
			$.ajax({
				type : "POST",
				dataType : "json",
				url : "{{ctx}}/mam/widget/checkName",
				data : "checkName=" + name,
				success : function(data, textStatus) {
					if (data.result == "true") {
						$("#name").attr("btvd-type","required");
						 $("#name").attr("btvd-err","该名称已经被占用，请换别的试试！");
					} else {
						$("#name").removeAttr("btvd-type");
						$("#name").removeAttr("btvd-err");
					}

				},
			});
		}

	}

	//更新已经设置自动打开模块的状态，设置为不自动打开
	function updateAutoShow(widgetId) {
		var data = {};
		data.id = widgetId;
		$.ajax({
			async : false,
			type : "POST",
			dataType : "json",
			url : "{{ctx}}/mam/widget/autoShowSatus",
			data : data,
			success : function(result, textStatus) {
				if (result.result == "success") {
					saveData();
				} else {
					bootbox.alert("保存失败，请稍后再试！");
				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});
	}

	//判断是否选择自动打开选项
	function isAutoShowCheck() {
		var widgetId = $('#id').val();
		var autoShow = $('#isAutoShowYes');
		if (autoShow.is(':checked')) {
			$.ajax({
				type : "GET",
				dataType : "json",
				url : "{{ctx}}/mam/widget/autoShow",
				success : function(data, textStatus) {
					if (data.result == "true") {
						if (widgetId != data.id) {
							bootbox.confirm("已经存在自动打开模块，您确定要将此模块设置为自动打开吗?",
									function(result) {
										if (result) {
											$('#autoShowResult').val(data.id);
										}else{
											$("input[name='autoShow']").get(0).checked = true;
										}
									});
						}
					}
				}
			});
			$('#timeUnit_isAutoShowYes').show();
			$('#showIntervalTime_isAutoShowYes').show();
		} else {
			$('#timeUnit_isAutoShowYes').hide();
			$('#showIntervalTime_isAutoShowYes').hide();
			$('#autoShowResult').val("");
		}
	}

	function checkNumber(id) {
		var reg = /^[0-9]\d*$/;
		var value = $('#' + id).val();
		var flag = reg.test(value);
		if (!flag) {
			$('#'+id).attr("btvd-type","required");
			 $('#'+id).attr("btvd-err","请输入数字");
		} else {
			$('#'+id).removeAttr("btvd-type");
			$('#'+id).removeAttr("btvd-err");
		}
	}
	
</script>

